package com.lhkj.ct.meta.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lhkj.ct.base.enums.GlobalStatus;
import com.lhkj.ct.base.model.Option;
import com.lhkj.ct.base.model.dto.PageQuery;
import com.lhkj.ct.base.mybatis.core.metadata.SqlOrder;
import com.lhkj.ct.base.mybatis.extension.service.impl.IServiceImpl;
import com.lhkj.ct.base.utils.OptionUtil;
import com.lhkj.ct.meta.admin.common.utils.DictUtils;
import com.lhkj.ct.meta.admin.mapper.SysDictDataMapper;
import com.lhkj.ct.meta.admin.model.entity.TblDictData;
import com.lhkj.ct.meta.admin.service.SysDictDataService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 字典数据表 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2023-09-07
 */
@Service
public class SysDictDataServiceImpl extends IServiceImpl<SysDictDataMapper, TblDictData> implements SysDictDataService {

    @Override
    public IPage<TblDictData> selectDictDataList(TblDictData dictData, PageQuery pageQuery) {
        LambdaQueryWrapper<TblDictData> wrapper = new LambdaQueryWrapper<TblDictData>()
                // 字典类型
                .eq(StringUtils.isNotBlank(dictData.getTypeCode()), TblDictData::getTypeCode, dictData.getTypeCode())
                // 字典标签
                .like(StringUtils.isNotBlank(dictData.getName()), TblDictData::getName, dictData.getName())
                // 字典状态
                .eq(Objects.nonNull(dictData.getStatus()), TblDictData::getStatus, dictData.getStatus());
        return getBaseMapper().selectPage(generatePage(pageQuery, SqlOrder.asc(TblDictData::getSort)), wrapper);
    }

    /**
     * 根据字典类型查询字典数据
     *
     * @param dictType 字典类型
     * @return 字典数据集合信息
     */
    @Override
    public List<Option<String>> selectDictDataByType(String dictType) {
        List<Option<String>> dictDatas = DictUtils.getDictOptionsCache(dictType);
        if (CollectionUtils.isNotEmpty(dictDatas)) {
            return dictDatas;
        }
        List<TblDictData> dictData = getBaseMapper().selectList(new LambdaQueryWrapper<TblDictData>()
                .eq(TblDictData::getTypeCode, dictType)
                .eq(TblDictData::getStatus, GlobalStatus.NORMAL.getValue())
                .orderByAsc(TblDictData::getSort)
        );
        dictDatas = OptionUtil.buildOption(dictData, TblDictData::getValue, TblDictData::getName);
        if (CollectionUtils.isNotEmpty(dictDatas)) {
            DictUtils.setDictOptionsCache(dictType, dictDatas);
            return dictDatas;
        }
        return new ArrayList<>();
    }

    @Override
    public boolean validated(String dictType, String dictValue) {
        if (StringUtils.isBlank(dictValue)) {
            return true;
        }
        List<Option<String>> dictData = selectDictDataByType(dictType);
        if (CollectionUtils.isEmpty(dictData)) {
            return false;
        }
        return dictData.stream().map(Option::getValue).anyMatch(item -> StringUtils.equals(dictValue, item));
    }
}
